package com.library.workbench.web.controller.user;

import com.library.commons.constant.Constant;
import com.library.commons.domain.ReturnObject;
import com.library.commons.utils.DateTimeUtils;
import com.library.settings.pojo.User;
import com.library.workbench.pojo.Book;
import com.library.workbench.pojo.Borrow;
import com.library.workbench.service.BookService;
import com.library.workbench.service.BorrowService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.util.*;

@Controller
public class ReturnController {
    @Autowired
    private BookService bookService;

    @Autowired
    private BorrowService borrowService;

    @RequestMapping("/workbench/user/return/toReturn.do")
    public String toReturn(){
        return "workbench/user/return/return";
    }

    @RequestMapping("/workbench/user/return/queryNeedReturnBookInfo.do")
    @ResponseBody
    public Object queryNeedReturnBookInfo(HttpSession session,String bookName, String bookAccount, String author){
        User user = (User) session.getAttribute(Constant.SESSION_USER);
        // 封装参数
        Map<String,Object> map=new HashMap<>();
        map.put("userId",user.getUserId());
        map.put("bookAccount",bookAccount);
        map.put("bookName",bookName);
        map.put("author",author);
        // 调用service层方法
        List<Borrow> borrows = borrowService.queryNeedReturnBorrowByConditionForPage(map);
        int totalRows = borrowService.queryNeedReturnBorrowByConditionForRows(map);
        map.put("borrows",borrows);
        map.put("totalRows",totalRows);
        // 返回响应信息
        return map;
    }

    @RequestMapping("/workbench/user/return/returnBook.do")
    @ResponseBody
    public Object returnBook(String borrowId) {
        ReturnObject returnObject = new ReturnObject();
        // 调用service层方法
        Borrow borrow = borrowService.queryBorrowById(borrowId);
        try {
            // 更新书籍信息
            Book book = bookService.queryBookByBookId(borrow.getBookId());
            book.setExist("1");
            int res = bookService.updateBookByBookId(book);

            // 更新归还信息
            borrow.setReturnTime(DateTimeUtils.formatDate(new Date()));
            if (borrow.getEndTime().compareTo(borrow.getReturnTime()) > 0) {
                borrow.setIslater("0");
            } else {
                borrow.setIslater("1");
            }
            res += borrowService.saveReturnBorrowInfo(borrow);
            if (res == 2) {
                returnObject.setCode(Constant.RETURN_SUCCESS);
            } else {
                returnObject.setCode(Constant.RETURN_FAIL);
                returnObject.setMessage("系统繁忙，请稍候再试....");
            }

        } catch (Exception e) {
            e.printStackTrace();
            returnObject.setCode(Constant.RETURN_FAIL);
            returnObject.setMessage("系统繁忙，请稍候再试....");
        }
        return returnObject;
    }
}
